package com.dev;


import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;

import java.io.IOException;

/**
 * <p>
 * https://gitee.com/archguide/zookeeper-demo
 * </p>
 *
 * <p>
 * https://gitee.com/archguide/zookeeper-demo.git
 * </p>
 *
 * @author : fujc-dev@qq.com
 * @motto : talk is cheap, show me the code. salute the future!
 */

public class Programs {
    /**
     * 入口函数
     *
     * @param args
     * @throws IOException
     * @throws InterruptedException
     * @throws KeeperException
     */
    public static void main(String[] args) throws IOException, InterruptedException, KeeperException {


        ZooKeeper zooKeeper = null;
        try {
            zooKeeper = new ZooKeeper("", 60 * 1000, new Watcher() {
                @Override
                public void process(WatchedEvent watchedEvent) {
                    System.out.println(watchedEvent);
                }
            });
            // 创建一个节点，并设置内容，设置ACL(该节点的权限设置)，
            // 节点类型（7种：持久节点、临时节点、持久顺序节点、临时顺序节点、容器节点、TTL节点、TTL顺序节点）
            String result = zooKeeper.create("/dev", "123".getBytes(),
                    ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            System.out.println(result);

            Stat stat = new Stat();
            byte[] data = zooKeeper.getData("/dev", false, stat);
            System.out.println(new String(data));
        } finally {
            if (zooKeeper != null) {
                zooKeeper.close();
            }
        }


        System.in.read();
    }
}
